package net.cdsunrise.com.auth.service;

import net.cdsunrise.com.auth.client.UserClient;
import net.cdsunrise.com.common.entity.SysUser;
import net.cdsunrise.com.common.grantedauthority.SpringGrantedAuthority;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import java.util.Set;

/**
 * @author: LiuWei
 * @date: Create in 14:56 2018/05/09
 */
@Service
public class DomainUserDetailsService implements UserDetailsService {

    @Autowired
    private UserClient userClient;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        SysUser realUser = userClient.get(username);
        if (realUser == null) {
            throw new UsernameNotFoundException("not dound");
        }
        Set<SpringGrantedAuthority> grantedAuthorities = realUser.getAuthorities();
        return new User(realUser.getUsername(), realUser.getPassword(), grantedAuthorities);
    }
}
